package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.AdminBean;
import com.woniuxy.educationplatform.bean.RoleBean;
import com.woniuxy.educationplatform.service.IAdminService;
import com.woniuxy.educationplatform.util.PageDto;
import com.woniuxy.educationplatform.util.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author unreleased
 * @date 2020/12/10 15:13
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private IAdminService adminServiceImpl;

    /**
     * 查询当前登录账户的相关信息
     * @return
     */
    @RequestMapping("/find")
    public ResultUtil find(){
        Subject subject = SecurityUtils.getSubject();
        String loginName = (String)subject.getPrincipal();
        AdminBean adminBan = adminServiceImpl.findAdminByLoginName(loginName);
        return new ResultUtil<AdminBean>().setCode(ResultUtil.SUCCESS).setData(adminBan);
    }

    /**
     * 管理员个人信息的修改
     * @param adminBean 当前操作的管理员 以及修改的信息
     * @return resultUtil
     */
    @RequestMapping("/update")
    public ResultUtil update(AdminBean adminBean){
        ResultUtil resultUtil = new ResultUtil();
        if(adminServiceImpl.updateAdminByLoginName(adminBean)){
            return resultUtil.setCode(ResultUtil.SUCCESS).setMessage("修改成功");
        }
        return resultUtil.setCode(ResultUtil.ERROR).setMessage("系统繁忙,请稍后再试");
    }

    /**
     * 管理员本人修改密码
     * @param oldPassword 原密码
     * @param newPassword 新密码
     * @param loginName 登录名
     * @return resultUtil
     */
    @PutMapping(value = {"update/{loginName}"})
    public ResultUtil updateAdminPwd(String oldPassword, String newPassword, @PathVariable String loginName){
        ResultUtil resultUtil = new ResultUtil();
        boolean b = adminServiceImpl.updateAdminPassword(loginName, oldPassword, newPassword);
        if(b){
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
            return resultUtil.setCode(ResultUtil.SUCCESS).setMessage("修改成功,请重新登录");
        }
        return resultUtil.setCode(ResultUtil.ERROR).setMessage("原密码有误，请重新输入");
    }

    /**
     *
     * @param adminBean 前端疯转的查询对象
     * @param pageNum 页码数
     * @param pageSize 每页大小
     * @param roleId 前端的角色id 由于不能自动封装，手动setRoleName
     * @return 返回结果
     */
    @RequestMapping("/findList")
    public ResultUtil<PageDto> findAllByParams(AdminBean adminBean,int pageNum,int pageSize,Integer roleId){
        adminBean.setRoleBean(new RoleBean().setId(roleId));
        PageDto data = adminServiceImpl.findAllByParams(adminBean, pageNum, pageSize);
        return new ResultUtil<PageDto>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }

    /**
     * 此方法用于超级管理员修改管理员的信息
     * @param adminBean 更改信息
     * @param roleId 角色信息
     * @return resultUtil
     */
    @PutMapping("/updateMsg")
    public ResultUtil updateAdminById(AdminBean adminBean,Integer roleId) {
        ResultUtil resultUtil = new ResultUtil();
        if(adminServiceImpl.updateAdminById(adminBean,roleId)){
            return resultUtil.setCode(ResultUtil.SUCCESS).setMessage("修改成功");
        }else {
            return resultUtil.setCode(ResultUtil.ERROR).setMessage("修改失败");
        }
    }

    /**
     * 超级管理删除管理员
     * @param id id
     * @return 结果
     */
    @DeleteMapping("/del/{id}")
    public ResultUtil delAdminById(@PathVariable Integer id){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
        if(adminServiceImpl.delAdminById(id)){
            resultUtil.setCode(ResultUtil.SUCCESS).setMessage("删除成功");
        }
        return  resultUtil;
    }

    /**
     * 实时检测新增以及修改管理员的时候名字是否重复
     * @param loginName
     * @param id
     * @return
     */
    @GetMapping("/check")
    public ResultUtil checkLoginNameIsRepeat(String loginName,Integer id) {
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("已经存在的用户名,请重新输入");
        if(!adminServiceImpl.checkLoginNameIsRepeat(loginName,id)){
            return resultUtil.setCode(ResultUtil.SUCCESS);
        }
        return resultUtil;
    }

    /**
     * 新增管理员
     * @param adminBean 携带信息的对象
     * @param roleId 角色id
     * @return 消息
     */
    @PostMapping("/saveAdmin")
    public ResultUtil saveAdmin (AdminBean adminBean,Integer roleId){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
        if(adminServiceImpl.saveAdmin(adminBean,roleId)){
            return resultUtil.setCode(ResultUtil.SUCCESS).setMessage("添加成功");
        }
        return resultUtil;
    }
}
